package com.hsource.paymentcenter.conf.redis;


import com.hsource.common.core.domain.message.MessageVO;
import io.netty.util.CharsetUtil;
import com.alibaba.fastjson.JSONObject;
import com.hsource.paymentcenter.conf.redis.PubSubTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/10/12 13:52
 * @describe
 */
@Component
public class RedisMessageProducer {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private PubSubTable pubSubTable;



    public void sendMessage(String topic, MessageVO messageVO) {
        //这里给订阅该主题的链接的每个队列进行广播该消息
        pubSubTable.boradCast(topic, messageVO.getMessageId());
        redisTemplate.getConnectionFactory().getConnection().publish(topic.getBytes(CharsetUtil.UTF_8), JSONObject.toJSONString(messageVO).getBytes());
    }

}
